<?php

namespace app\admin\model;

use app\model\Model;

class Detail extends Model
{
  public $privilege = 'detail';
  public $priv;
  public $cids;
  public $id;
  public $cid;
  public $type;
  public $keyword = '';
  public $page = 1;
  public $param;
  public $db;

  function __construct()
  {
    include 'check.php';
    new Check();

    checkAdminPriv(@$this->privilege); // 判断权限
    $this->db = $GLOBALS['db'];
    $this->priv = priv_id($this->db->getOne("SELECT r.r_priv FROM user AS u INNER JOIN role AS r ON r.id=u.u_rid WHERE u.id = " . get_admin_token('id')));
    $this->id = intval($_GET['id']);
    $this->page = max(intval($_GET['page']), 1);
  }

  function index()
  {
    include_once LIB_PATH . 'cls.page.php';

    $this->cid = intval($_GET['cid']);
    $this->type = isset($_GET['type']) ? str_safe($_GET['type']) : 'all';
    $this->keyword = isset($_GET['keyword']) ? str_safe($_GET['keyword']) : '';

    $where = "WHERE id > 0" . (!empty($this->keyword) ? " AND d_name LIKE '%" . $this->keyword . "%'" : "");
    if (!empty($this->cid)) {
      $where .= get_channel($this->cid, 'c_sub') ? " AND d_parent IN (" . get_channel($this->cid, 'c_sub') . ")" : " AND d_parent IN (" . $this->cid . ")";
    } else {
      if ($this->cids) {
        $where .= " AND d_parent IN (" . $this->cids . ")";
      }
    }

    switch ($this->type) {
      case 'rec':
        $where .= " AND d_rec = 1";
        break;
      case 'hot':
        $where .= " AND d_hot = 1";
        break;
      case 'enable':
        $where .= " AND d_enable = 1";
        break;
      case 'disable':
        $where .= " AND d_enable = 0";
        break;
      default:
        break;
    }
    $pager = new \Page(ADMIN_PAGE_SIZE);
    $pager->handle($GLOBALS['db']->getOne("SELECT COUNT(*) FROM detail $where"));

    $this->tpl('detail', [
      'db' => $this->db,
      'list' => $GLOBALS['db']->getAll("SELECT * FROM detail $where ORDER BY id DESC LIMIT " . $pager->page_start . "," . $pager->page_size),
      'pager' => $pager,
    ]);
  }

  function batch()
  {
    null_back(@$_POST['id'], '请至少选中一项！');
    $id = array_str($_POST['id']);

    // 批处理
    if (isset($_POST['execute'])) {

      switch ($_POST['execute_method']) {
        case 'srec':
          $sql = "UPDATE detail SET d_rec = 1 WHERE id in ($id)";
          break;
        case 'crec':
          $sql = "UPDATE detail SET d_rec = 0 WHERE id in ($id)";
          break;
        case 'shot':
          $sql = "UPDATE detail SET d_hot = 1 WHERE id in ($id)";
          break;
        case 'chot':
          $sql = "UPDATE detail SET d_hot = 0 WHERE id in ($id)";
          break;
        case 'spop':
          $sql = "UPDATE detail SET d_popup = 1 WHERE id in ($id)";
          break;
        case 'cpop':
          $sql = "UPDATE detail SET d_popup = 0 WHERE id in ($id)";
          break;
        case 'enable':
          $sql = "UPDATE detail SET d_enable = 1 WHERE id in ($id)";
          break;
        case 'disable':
          $sql = "UPDATE detail SET d_enable = 0 WHERE id in ($id)";
          break;
        case 'delete':
          $sql = "DELETE FROM detail WHERE id IN ($id)";
          break;
        default:
          alert_back('请选择要执行的操作');
      }

      if ($GLOBALS['db']->exec($sql)) {
        switch ($_POST['execute_method']) {
          case 'srec':
            admin_log('详情推荐开启 [' . $id . ']');
            break;
          case 'crec':
            admin_log('详情推荐关闭 [' . $id . ']');
            break;
          case 'shot':
            admin_log('详情热门开启 [' . $id . ']');
            break;
          case 'chot':
            admin_log('详情热门关闭 [' . $id . ']');
            break;
          case 'spop':
            admin_log('详情弹窗开启 [' . $id . ']');
            break;
          case 'cpop':
            admin_log('详情弹窗关闭 [' . $id . ']');
            break;
          case 'enable':
            admin_log('详情开启 [' . $id . ']');
            break;
          case 'disable':
            admin_log('详情关闭 [' . $id . ']');
            break;
          case 'delete':
            admin_log('批量详情信息删除 [' . $id . ']');
            break;
          default:
            break;
        }

        alert_href($GLOBALS['lang']['msg_success'], M('admin', 'detail'));
      }
    }

    // 转移
    if (isset($_POST['shift'])) {
      null_back($_POST['shift_target'], '请选择要转移到的频道');

      if ($GLOBALS['db']->exec("UPDATE detail SET d_parent = " . str_safe($_POST['shift_target']) . " WHERE id IN ($id)")) {
        admin_log("详情转移 [$id]");
        alert_href($GLOBALS['lang']['msg_success'], M('admin', 'detail'));
      }
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);
  }

  function add()
  {
    $this->tpl('detail_add', [
      'db' => $this->db
    ]);
  }

  function add_action()
  {
    $data = get_post(['d_tag', 'd_content', 'd_scontent', 'd_source', 'd_author', 'd_sdate', 'd_edate', 'd_picture', 'd_slideshow', 'd_attachment', 'd_link', 'd_seoname', 'd_keywords', 'd_description'], false);

    $data['d_ifpicture'] = !empty($data['d_picture']) ? 1 : 0;
    $data['d_ifslideshow'] = !empty($data['d_slideshow']) ? 1 : 0;
    $data['d_ifvideo'] = !empty($data['d_video']) ? 1 : 0;
    $data['d_ifattachment'] = !empty($data['d_attachment']) ? 1 : 0;
    $data['d_hits'] = 0;

    if ($id = $GLOBALS['db']->exec("INSERT INTO detail " . arr_insert($data))) {
      admin_log("详情新增 [$id]");
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'detail'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);

    /*
    $data['d_name'] = str_safe($_POST['d_name']);
    $data['d_picture'] = str_safe($_POST['d_picture']);
    $data['d_ifpicture'] = !empty($data['d_picture']) ? 1 : 0;
    $data['d_parent'] = intval($_POST['d_parent']);
    $data['d_rec'] = intval($_POST['d_rec']);
    $data['d_hot'] = intval($_POST['d_hot']);
    $data['d_slideshow'] = !empty($_POST['d_slideshow']) ? str_safe($_POST['d_slideshow']) : '';
    $data['d_ifslideshow'] = !empty($data['d_slideshow']) ? 1 : 0;
    $data['d_video'] = !empty($_POST['d_video']) ? str_safe($_POST['d_video']) : '';
    $data['d_ifvideo'] = !empty($data['d_video']) ? 1 : 0;
    $data['d_attachment'] = str_safe($_POST['d_attachment']);
    $data['d_ifattachment'] = !empty($data['d_attachment']) ? 1 : 0;
    $data['d_content'] = str_isafe($_POST['d_content']);
    $data['d_scontent'] = str_isafe($_POST['d_scontent']);
    $data['d_source'] = str_safe($_POST['d_source']);
    $data['d_author'] = str_safe($_POST['d_author']);
    $data['d_seoname'] = str_safe($_POST['d_seoname']);
    $data['d_keywords'] = str_safe($_POST['d_keywords']);
    $data['d_description'] = str_safe($_POST['d_description']);
    $data['d_link'] = str_safe($_POST['d_link']);
    $data['d_date'] = local_strtotime($_POST['d_date']);
    $data['d_sdate'] = !empty($_POST['d_sdate']) ? local_strtotime($_POST['d_sdate']) : 0;
    $data['d_enable'] = $data['d_sdate'] > 0 ? 0 : 1;
    $data['d_edate'] = !empty($_POST['d_edate']) ? local_strtotime($_POST['d_edate']) : 0;
    $data['d_order'] = intval($_POST['d_order']);
    $data['d_hits'] = 1;
    $data['d_tag'] = str_safe($_POST['d_tag']);

    //判断相关数据
    null_back($data['d_name'], '详情名称不能为空');
    null_back($data['d_parent'], '请选择上级频道');
    non_numeric_back($data['d_order'], '排序必须是数字!');
    if ($data['d_ifpicture'] == 1) {
      null_back($data['d_picture'], '请填写图片标题');
    }
    if ($data['d_ifslideshow'] == 1) {
      null_back($data['d_slideshow'], '请上传组图');
    }
    if ($data['d_ifattachment'] == 1) {
      null_back($data['d_attachment'], '请上传附件');
    }
    if ($data['d_ifvideo'] == 1) {
      null_back($data['d_video'], '请填写视频');
    }
    */
  }

  function edit()
  {
    $this->tpl('detail_edit', [
      'db' => $this->db,
      'row' => $GLOBALS['db']->getRow("SELECT d_name,d_parent,d_rec,d_hot,d_tag,d_content,d_scontent,d_source,d_author,d_date,d_sdate,d_edate,d_order,d_picture,d_slideshow,d_attachment,d_link,d_seoname,d_keywords,d_description FROM detail WHERE id = $this->id")
    ]);
  }

  function edit_action()
  {
    $data = get_post(['d_tag', 'd_content', 'd_scontent', 'd_source', 'd_author', 'd_sdate', 'd_edate', 'd_picture', 'd_slideshow', 'd_attachment', 'd_link', 'd_seoname', 'd_keywords', 'd_description'], false);
    // dd($data);

    $data['d_ifpicture'] = !empty($data['d_picture']) ? 1 : 0;
    $data['d_picture'] = !empty($data['d_picture']) ? $data['d_picture'] : '';
    $data['d_ifslideshow'] = !empty($data['d_slideshow']) ? 1 : 0;
    $data['d_slideshow'] = !empty($data['d_slideshow']) ? $data['d_slideshow'] : '';
    $data['d_ifvideo'] = !empty($data['d_video']) ? 1 : 0;
    $data['d_video'] = !empty($data['d_video']) ? $data['d_video'] : '';
    $data['d_ifattachment'] = !empty($data['d_attachment']) ? 1 : 0;
    $data['d_attachment'] = !empty($data['d_attachment']) ? $data['d_attachment'] : '';
    $data['d_tag'] = !empty($data['d_tag']) ? $data['d_tag'] : '';
    $data['d_content'] = !empty($data['d_content']) ? $data['d_content'] : '';
    $data['d_scontent'] = !empty($data['d_scontent']) ? $data['d_scontent'] : '';
    $data['d_source'] = !empty($data['d_source']) ? $data['d_source'] : '';
    $data['d_author'] = !empty($data['d_author']) ? $data['d_author'] : '';
    $data['d_link'] = !empty($data['d_link']) ? $data['d_link'] : '';
    $data['d_seoname'] = !empty($data['d_seoname']) ? $data['d_seoname'] : '';
    $data['d_keywords'] = !empty($data['d_keywords']) ? $data['d_keywords'] : '';
    $data['d_description'] = !empty($data['d_description']) ? $data['d_description'] : '';
    // dd($data);

    if ($GLOBALS['db']->exec("UPDATE detail SET " . arr_update($data) . " WHERE id = $this->id")) {
      admin_log("详情编辑 [$this->id]");
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'detail') . "&page=$this->page");
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);

    /*
    $data['d_name'] = str_safe($_POST['d_name']);
    $data['d_picture'] = str_safe($_POST['d_picture']);
    $data['d_ifpicture'] = !empty($data['d_picture']) ? 1 : 0;
    $data['d_parent'] = intval($_POST['d_parent']);
    $data['d_rec'] = intval($_POST['d_rec']);
    $data['d_hot'] = intval($_POST['d_hot']);
    $data['d_slideshow'] = !empty($_POST['d_slideshow']) ? str_safe($_POST['d_slideshow']) : '';
    $data['d_ifslideshow'] = !empty($data['d_slideshow']) ? 1 : 0;
    $data['d_video'] = !empty($_POST['d_video']) ? str_safe($_POST['d_video']) : '';
    $data['d_ifvideo'] = !empty($data['d_video']) ? 1 : 0;
    $data['d_attachment'] = str_safe($_POST['d_attachment']);
    $data['d_ifattachment'] = !empty($data['d_attachment']) ? 1 : 0;
    $data['d_content'] = str_isafe($_POST['d_content']);
    $data['d_scontent'] = str_isafe($_POST['d_scontent']);
    $data['d_source'] = str_safe($_POST['d_source']);
    $data['d_author'] = str_safe($_POST['d_author']);
    $data['d_seoname'] = str_safe($_POST['d_seoname']);
    $data['d_keywords'] = str_safe($_POST['d_keywords']);
    $data['d_description'] = str_safe($_POST['d_description']);
    $data['d_link'] = str_safe($_POST['d_link']);
    $data['d_date'] = local_strtotime($_POST['d_date']);
    $data['d_sdate'] = $_POST['d_sdate'] > 0 ? local_strtotime($_POST['d_sdate']) : 0;
    $data['d_enable'] = $data['d_sdate'] > 0 ? 0 : 1;
    $data['d_edate'] = $_POST['d_edate'] > 0 ? local_strtotime($_POST['d_edate']) : 0;
    $data['d_order'] = intval($_POST['d_order']);
    $data['d_tag'] = str_safe($_POST['d_tag']);

    null_back($data['d_name'], '详情名称不能为空');
    non_numeric_back($data['d_order'], '排序必须是数字!');
    */
  }

  function __destruct()
  {
  }
}